home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 3.iso / dist / fw_mc.idb / usr / freeware / lib32 / mc / extfs / ulha.z / ulha
Text File  |  2002-07-08  |  5KB  |  165 lines

  1. #! /bin/sh
  2.  
  3. #
  4. # LHa Virtual filesystem executive v0.1
  5. # Copyright (C) 1996, 1997 Joseph M. Hinkle
  6. # May be distributed under the terms of the GNU Public License
  7. # <jhinkle@rockisland.com>
  8. #
  9.  
  10. # Code for mc_lha_fs_run() suggested by:
  11. # Jan 97    Zdenek Kabelac <kabi@informatics.muni.cz>
  12.  
  13. # Tested with mc 3.5.18 and gawk 3.0.0 on Linux 2.0.0
  14. # Tested with lha v1.01 and lharc v1.02
  15. # Information and sources for other forms of lha/lzh appreciated
  16.  
  17. # Nota bene:
  18. # There are several compression utilities which produce *.lha files.
  19. # LHArc and LHa in exist several versions, and their listing output varies.
  20. # Another variable is the architecture on which the compressed file was made.
  21. # This program attempts to sort out the variables known to me, but it is likely
  22. # to display an empty panel if it encounters a mystery. 
  23. # In that case it will be useful to execute this file from the command line:
  24. # ./lha list Mystery.lha
  25. # to examine the output directly on the console.  The output string must be
  26. # precisely in the format described in the README in this directory.
  27. # Another helpful thing is to temporarily remove the redirection of error
  28. # output of awk (The '2> /dev/null' instruction near the end of mcfs_list())
  29. # The screen will get ugly if there's an error, but some useful text shows
  30. # at the bottom of the screen.  
  31. # Caveat emptor.
  32. # Learn Latin.
  33.  
  34. # Define your awk
  35. AWK=nawk
  36.  
  37. if ls -de . >& /dev/null;
  38. then
  39.     LS_COMMAND="ls -le"
  40. else
  41.     LS_COMMAND="ls -l --full-time"
  42. fi
  43.  
  44. # Define which archiver you are using with appropriate options
  45. LHA_LIST="lha l"
  46. LHA_GET="lha pq"
  47. LHA_PUT="lha aq"
  48.  
  49. # Define a directory to create a temporary file for when
  50. # running a command to be run from the archive
  51. TMPDIR=/tmp/mc-cmd.$$
  52. # Temporary file within the directory
  53. TMPCMD=$TMPDIR/run
  54.  
  55. # The 'list' command executive
  56.  
  57. mc_lha_fs_list()
  58. {
  59.    # Get the year of the file timestamp in case we need to replace 'hh:mm'
  60.    YEAR=$($LS_COMMAND $1 | $AWK '{ print $10 }')
  61.    # List the contents of the archive and sort it out    
  62.    $LHA_LIST $1 | $AWK -v uid=$(id -nu) -v gid=$(id -ng) -v year=$YEAR '
  63.       # Ignore the annotations, quit on the last one
  64.       /^\ PERMSSN/ { next }
  65.       /^-----/ { next }
  66.       /^\ Total/ { exit 0 }
  67.       # Strip a leading '/' if present in a filepath
  68.       $(NF) ~ /^\// { $(NF) = substr($NF,2) }
  69.       # Replace the year stamp if it is expressed as 'hh:mm'
  70.       $(NF-1) ~ /\:/ { $(NF-1) = year }
  71.       # Print the line this way if there is no permission string
  72.       $1 ~ /^\[generic\]/ {
  73.          # Invent a generic permission
  74.          $1 = ($10 ~ /\/$/) ? "drwxr-xr-x":"-rwxr--r--";
  75.          # Print it
  76.          printf "%s   1 %-8s %-8s %-8d %3s %2d %4d %s\n",
  77.                  $1,     uid, gid,  $2, $4, $5, $6, $7;
  78.          # Get the next line of the list
  79.          next;
  80.       }
  81.       # Do it this way for a defined permission
  82.       $1 !~ /^\[generic\]/ {
  83.          # If the permissions and UID run together
  84.          if ($1 ~ /\//) {
  85.             $8 = $7;
  86.             $7 = $6;
  87.             $6 = $5;
  88.             $5 = $4;
  89.             $3 = $2;
  90.             $2 = substr($1,10);
  91.             $1 = substr($1,1,9);
  92.          }
  93.          # If the permission string is missing a type
  94.          if (length($1) == 9) {
  95.             if ($NF ~ /\/$/)
  96.                $1 = ("d" $1);
  97.             else
  98.                $1 = ("-" $1);
  99.          }
  100.          # UID:GID might not be the same as on your system so print numbers
  101.          # Well, that is the intent.  At the moment mc is translating them.
  102.          split($2, id, "/");
  103.          printf "%s   1 %-8d %-8d %-8d %3s %2d %4d %s\n",
  104.                  $1,     id[1], id[2],  $3, $5, $6, $7, $8;
  105.          # Get the next line of the list
  106.          next;
  107.       }
  108.  
  109.    ' 2> /dev/null
  110. }
  111.  
  112. # The 'copyout' command executive to copy displayed files to a destination
  113.  
  114. mc_lha_fs_copyout()
  115. {
  116.    $LHA_GET $1 $2 > $3  2> /dev/null
  117. }
  118.  
  119. # The 'copyin' command executive to add something to the archive
  120.  
  121. mc_lha_fs_copyin ()
  122. {
  123.    NAME2=`basename $2`; DIR2=${2%$NAME2}
  124.    NAME3=`basename $3`; DIR3=${3%$NAME3}
  125.  
  126.    cd ${DIR3}
  127.  
  128.    ONE2=${2%%/*}
  129.    [ -n ${ONE2} ] || exit
  130.    [ -e ${ONE2} ] && exit
  131.  
  132.    [ -e ${DIR2} ] || mkdir -p ${DIR2}
  133.    ln $3 $2       || exit
  134.  
  135.    $LHA_PUT $1 $2  2> /dev/null
  136.    rm -r ${ONE2}
  137. }
  138.  
  139. # The 'run' command executive to run a command from within an archive
  140.  
  141. mc_lha_fs_run()
  142. {
  143.    trap "rm $TMPCMD; rmdir $TMPDIR; exit 0" 1 2 3 4 15
  144. # FIXME: Try harder to generate a unique directory if this fails
  145.    mkdir -m 0700 $TMPDIR || exit 1
  146.    $LHA_GET $1 $2 > $TMPCMD  2> /dev/null
  147.    chmod a+x $TMPCMD  2> /dev/null
  148.    $TMPCMD 2> /dev/null
  149.    rm $TMPCMD
  150.    rmdir $TMPDIR
  151. }
  152.  
  153.  
  154. # The main routine
  155. umask 077
  156.  
  157. case "$1" in
  158.    list) mc_lha_fs_list $2; exit $?;;
  159.    copyout) mc_lha_fs_copyout $2 $3 $4; exit $?;;
  160.    copyin)  mc_lha_fs_copyin  $2 $3 $4; exit $?;;
  161.    run)     mc_lha_fs_run     $2 $3 $4; exit $?;;
  162. esac
  163. exit 1
  164.  
  165.